之前在Hashicorp forum看到一個發問 Can a service advertise multiple ports? 所以記錄下來。
User在一個task裡使用2個port, port name
分別是http, metrics, 再向Consul註冊一個service, service name
為awesome-api, 想要達到2個port註冊相同的service,
一般來說會為註冊不同的service name如:
...
service {
name = "awesome-api-http"
port = "http"
}
service {
name = "awesome-api-metrics"
port = "metrics"
}
...
User的寫法如下:
...
network {
port "http" { host_network = "private" }
port "metrics" {
host_network = "private"
static = "9090"
}
//port "metrics" { host_network = "private" }
}
service {
name = "awesome-api"
port = "http"
//port = "metrics"
}
...
把註解拿掉會報錯
Dimension network: reserved port collision metrics=9090 exhausted on [x] nodes
有網友回復可以使用tag
來達到這需求:
service {
name = "awesome-api"
port = "http"
tags = ["http"]
}
service {
name = "awesome-api"
port = "metrics"
tags = ["metrics"]
}
訪問的endpoint會變成 http.awesome-api.service.consul
與 metrics.awesome-api.service.consul
這樣即可達成一個service多個port的需求